//
//  WaitingRepairTableViewCell5.m
//  GetPowerDBO
//
//  Created by 周博 on 2017/8/11.
//  Copyright © 2017年 BogoZhou. All rights reserved.
//

#import "WaitingRepairTableViewCell5.h"
#import "RepairDetailV2ViewController.h"


@implementation WaitingRepairTableViewCell5

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code

}


- (void)showDataWithModel:(WaitingRepairModel *)model andType:(NSString *)type{
    
    for (UIView *view in self.subviews) {
        [view removeFromSuperview];
    }
    
    
    
    model.shop = [[ShopModel alloc] init];
    [model.shop setValuesForKeysWithDictionary:model.store_info];
    
    _myModel = model;
    _storeType = model.shop.store_type;
#pragma mark - 顶部商家信息View
    _viewShop = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, 125)];
    
    //imageViewHeader
    _imageViewHeader = [[UIImageView alloc] initWithFrame:CGRectMake(10, (125 - 80)/2, 80, 80)];
    [_imageViewHeader sd_setImageWithURL:[NSURL URLWithString:model.shop.img_url] placeholderImage:[UIImage imageNamed:@""]];
    [_viewShop addSubview:_imageViewHeader];
    
    _imageViewPowerType = [[UIImageView alloc] initWithFrame:CGRectMake(100, 12.5, 20, 20)];
    if (model.shop.store_type.integerValue == 4) {
        _imageViewPowerType.image = [UIImage imageNamed:@"28_mimasuo"];
    }else{
        _imageViewPowerType.image = [UIImage imageNamed:@"28_WiFi"];
    }
    [_viewShop addSubview:_imageViewPowerType];
    
    //店名
    _labelShopName = [[UILabel alloc] initWithFrame:CGRectMake(125, 12.5, kScreenSize.width - 125 - 10, 20)];
    [self setLabelContent:_labelShopName labelString:model.shop.name];
    [_viewShop addSubview:_labelShopName];
    
    //店主
    _imageViewMer = [[UIImageView alloc] initWithFrame:CGRectMake(100, _labelShopName.allHeight + 5, 20, 20)];
    _imageViewMer.contentMode = UIViewContentModeScaleAspectFit;
    _imageViewMer.image = [UIImage imageNamed:@"lianxiren"];
    [_viewShop addSubview:_imageViewMer];
    
    _labelMerName = [[UILabel alloc] initWithFrame:CGRectMake(_imageViewMer.allWidth +2, _imageViewMer.orginY, kScreenSize.width - _imageViewMer.allWidth -2 - 120, 20)];
    [self setLabelContent:_labelMerName labelString:model.shop.contact];
    [_viewShop addSubview:_labelMerName];
    
    //设备总数
    _imageViewZong = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenSize.width - 120, _labelShopName.orginY, 20, 20)];
    _imageViewZong.image = [UIImage imageNamed:@"28_zong"];
    [_viewShop addSubview:_imageViewZong];
    
    _labelZong = [[UILabel alloc] initWithFrame:CGRectMake(kScreenSize.width - 95, _labelShopName.orginY, 95, 20)];
    [_viewShop addSubview:_labelZong];
    
    
    //电话
    _imageViewTel = [[UIImageView alloc] initWithFrame:CGRectMake(100, _labelMerName.allHeight + 5, 20, 20)];
    _imageViewTel.contentMode = UIViewContentModeScaleAspectFit;
    _imageViewTel.image = [UIImage imageNamed:@"dianhua"];
    [_viewShop addSubview:_imageViewTel];
    
    _labelTel = [[UILabel alloc] initWithFrame:CGRectMake(_imageViewMer.allWidth +2, _imageViewTel.orginY, kScreenSize.width - _imageViewMer.allWidth -2 - 120, 20)];
    [self setLabelContent:_labelTel labelString:model.shop.tel];
    [_viewShop addSubview:_labelTel];
    
    //店铺状态
    _imageViewType = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenSize.width - 120, _imageViewMer.orginY, 20, 20)];
    _imageViewType.image = [UIImage imageNamed:@"mendian"];
    [_viewShop addSubview:_imageViewType];
    
    _labelType = [[UILabel alloc] initWithFrame:CGRectMake(kScreenSize.width - 95, _imageViewMer.orginY, 95, 20)];
    [self setLabelContent:_labelType labelString:[NSString stringWithFormat:@"%@",model.status.integerValue == 0 ? @"待维护" : @"维护成功"]];
    [_viewShop addSubview:_labelType];
    
    //今日或昨日
//    _imageViewJinOrZuo = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenSize.width - 120, _imageViewTel.orginY, 20, 20)];
//
//    [_viewShop addSubview:_imageViewJinOrZuo];

    _labelJinOrZuo = [[UILabel alloc] initWithFrame:CGRectMake(kScreenSize.width - 120, _imageViewTel.orginY, 120, 20)];
    [_viewShop addSubview:_labelJinOrZuo];
    
    _labelJinOrZuo.textColor = kGreenColor;
    
    NSString * jinriOZuori;
    if (model.status.integerValue == 0) {//昨日满2小时
//        _imageViewJinOrZuo.image = [UIImage imageNamed:@"14_zuo"];
        jinriOZuori = @"昨日";
    }else{//今日满2小时
//        _imageViewJinOrZuo.image = [UIImage imageNamed:@"14_jin"];
        jinriOZuori = @"当日";

    }
    
    if (model.shop.store_type.integerValue == 4) {//密码设备
        
        
        [self setLabelContent:_labelZong labelString:model.mima_already_num];
        [self setLabelContent:_labelJinOrZuo labelString:[NSString stringWithFormat:@"%@设备详情：%@",jinriOZuori,model.mima_ok_num]];
    }else{
        [self setLabelContent:_labelZong labelString:model.already_num];
        [self setLabelContent:_labelJinOrZuo labelString:[NSString stringWithFormat:@"%@设备详情：%@",jinriOZuori,model.ok_num]];
    }
    
    //地址
    _imageViewAddress = [[UIImageView alloc] initWithFrame:CGRectMake(100, _imageViewTel.allHeight + 5, 20, 20)];
    _imageViewAddress.contentMode = UIViewContentModeScaleAspectFit;
    _imageViewAddress.image = [UIImage imageNamed:@"dizhi"];
    [_viewShop addSubview:_imageViewAddress];
    
    _labelAddress = [[UILabel alloc] init];
    
    _labelAddress.numberOfLines = 0;
    if ([model.date isEqualToString:[BGDateHelper seeDayAroundToday:0][0]] || [model.date isEqualToString:[BGDateHelper seeDayAroundToday:1][0]]) {
        
        CGSize titleSize = [model.shop.specific_address boundingRectWithSize:CGSizeMake(kScreenSize.width - _imageViewMer.allWidth -2 - 10, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size;
        
        _labelAddress.frame = CGRectMake(_imageViewMer.allWidth +2, _imageViewAddress.orginY, kScreenSize.width - _imageViewMer.allWidth -2 - 10, titleSize.height);
        
    }else{
        
        
        
        if (model.status.integerValue == 0) {//待维护
            CGSize titleSize = [model.shop.specific_address boundingRectWithSize:CGSizeMake(_imageViewType.allWidth - _imageViewAddress.allWidth  - 25, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size;
            
            _labelAddress.frame = CGRectMake(_imageViewMer.allWidth +2, _imageViewAddress.orginY, _imageViewType.allWidth - _imageViewAddress.allWidth  - 25, titleSize.height);
            
            _labelPowerDetail = [[UILabel alloc] initWithFrame:CGRectMake(_imageViewType.orginX, _labelAddress.orginY, kScreenSize.width - _imageViewType.allWidth -2 - 10, 20)];
            [self setLabelContent:_labelPowerDetail labelString:@"当日设备详情"];
            _labelPowerDetail.textColor = kGreenColor;
            [_viewShop addSubview:_labelPowerDetail];
        }else{
            CGSize titleSize = [model.shop.specific_address boundingRectWithSize:CGSizeMake(kScreenSize.width - _imageViewMer.allWidth -2 - 10, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size;
            
            _labelAddress.frame = CGRectMake(_imageViewMer.allWidth +2, _imageViewAddress.orginY, kScreenSize.width - _imageViewMer.allWidth -2 - 10, titleSize.height);
        }
        
    }
    
    [self setLabelContent:_labelAddress labelString:model.shop.specific_address];
    [_viewShop addSubview:_labelAddress];
    
    
    //店铺当日是否维护了
    _jinriStatus = model.maintained_status;
    
    if (_jinriStatus.integerValue == 1) {
        _ids = model.wifi_already_maintained_ids;
//        _imageViewJinOrZuo.image = [UIImage imageNamed:@"14_jin"];
        [self setLabelContent:_labelJinOrZuo labelString:[NSString stringWithFormat:@"当日设备详情：%@",model.wifi_already_maintained_num]];
        _labelPowerDetail.hidden = YES;
    }else{
        if (model.shop.store_type.integerValue == 4) {//mima设备
            
        }else{
            _ids = model.charging_info_ids;

        }
    }
    
    _dateString = model.date;
    _status = model.status;
    
    _viewShop.frame = CGRectMake(_viewShop.orginX, _viewShop.orginY, _viewShop.sizeWidth, _labelAddress.allHeight + 12.5);
    
    [self addSubview:_viewShop];
//    if (type.integerValue == 1) {//今天
//        
//    }else{//其他日期
//        
//    }
//    
//    if (model.status.integerValue == 0) {//昨日满2小时
//        
//    }else{//昨日满2小时
//
//    }
    
//    _line1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 124, kScreenSize.width, 1)];
//    _line1.backgroundColor = kColorFrom0x(0xeeeeee);
//    [_viewShop addSubview:_line1];
//    
    
//
//#pragma mark - 下方WiFi信息
//    _viewWifi = [[UIView alloc] init];
//    
//    if (model.already_num.integerValue == 0) {
//        _viewWifi.frame = CGRectMake(0, _viewShop.sizeHeight, kScreenSize.width, 0);
//    }else{
//        
//        if (type.integerValue == 1) {//今天
//            _viewWifi.frame = CGRectMake(0, _viewShop.sizeHeight, kScreenSize.width, 60);
//            _imageViewWifi = [[UIImageView alloc] initWithFrame:CGRectMake(10, 20, 20, 20)];
//
//        }else{//其他日期
//            _viewWifi.frame = CGRectMake(0, _viewShop.sizeHeight, kScreenSize.width, 40);
//            _imageViewWifi = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 20, 20)];
//
//        }
//        
//        _imageViewWifi.contentMode = UIViewContentModeScaleAspectFit;
//        _imageViewWifi.image = [UIImage imageNamed:@"28_WiFi"];
//        [_viewWifi addSubview:_imageViewWifi];
//        
//        CGFloat lineOneWidth = (kScreenSize.width - 40)/2;
//        CGFloat lineTwoWidth = (kScreenSize.width - 40)/3;
//        
//        //wifi 已铺
//        _labelWifiYipu = [[UILabel alloc] initWithFrame:CGRectMake(40, 10, lineOneWidth, 20)];
//        [self setLabelContent:_labelWifiYipu labelString:[NSString stringWithFormat:@"已铺：%@",model.already_num]];
//        [_viewWifi addSubview:_labelWifiYipu];
//        
//        //wifi 昨日满2小时
//        _labelWifiDetail = [[UILabel alloc] initWithFrame:CGRectMake(40 + lineOneWidth, 10, lineOneWidth, 20)];
//        if (model.status.integerValue == 0) {
//            [self setLabelContent:_labelWifiDetail labelString:[NSString stringWithFormat:@"昨日满2小时：%@",model.ok_num]];
//        }else{
//            [self setLabelContent:_labelWifiDetail labelString:[NSString stringWithFormat:@"今日满2小时：%@",model.ok_num]];
//
//        }
//        [_viewWifi addSubview:_labelWifiDetail];
//        
//        if (type.integerValue == 1) {
//            //实时在线
//            _labelWifiOnline = [[UILabel alloc] initWithFrame:CGRectMake(40, 30, lineTwoWidth, 20)];
//            [self setLabelContent:_labelWifiOnline labelString:[NSString stringWithFormat:@"实时在线：%@",model.online_num]];
//            [_viewWifi addSubview:_labelWifiOnline];
//            
//            //实时离线
//            _labelWifiOffLine = [[UILabel alloc] initWithFrame:CGRectMake(40 + lineTwoWidth, 30, lineTwoWidth, 20)];
//            [self setLabelContent:_labelWifiOffLine labelString:[NSString stringWithFormat:@"实时离线：%@",model.offline_num]];
//            [_viewWifi addSubview:_labelWifiOffLine];
//            
//            //平均在线
//            _labelAvgOnline = [[UILabel alloc] initWithFrame:CGRectMake(40 + lineTwoWidth *2, 30, lineTwoWidth, 20)];
//            [self setLabelContent:_labelAvgOnline labelString:[NSString stringWithFormat:@"平均在线：%@",model.avg_online_num]];
//            [_viewWifi addSubview:_labelAvgOnline];
//        }
//        
//
//        _line2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, _viewWifi.sizeHeight - 1 , kScreenSize.width, 1)];
//        _line2.backgroundColor = kColorFrom0x(0xeeeeee);
//        [_viewWifi addSubview:_line2];
//    }
//    [self addSubview:_viewWifi];
//#pragma mark - 下方密码信息
//    _viewMima = [[UIView alloc] init];
//    
//    if (model.mima_already_num.integerValue == 0) {
//        _viewMima.frame = CGRectMake(0, _viewWifi.allHeight, kScreenSize.width, 0);
//    }else{
//        _viewMima.frame = CGRectMake(0, _viewWifi.allHeight, kScreenSize.width, 40);
//        
//        _imageViewMima = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 20, 20)];
//        _imageViewMima.contentMode = UIViewContentModeScaleAspectFit;
//        _imageViewMima.image = [UIImage imageNamed:@"28_mimasuo"];
//        [_viewMima addSubview:_imageViewMima];
//        
//        CGFloat lineOneWidth = (kScreenSize.width - 40)/2;
//        //密码 已铺
//        _labelMimaYipu = [[UILabel alloc] initWithFrame:CGRectMake(40, 10, lineOneWidth, 20)];
//        [self setLabelContent:_labelMimaYipu labelString:[NSString stringWithFormat:@"已铺：%@",model.mima_already_num]];
//        [_viewMima addSubview:_labelMimaYipu];
//        
//        //密码使用
//        _labelMimaShiyong = [[UILabel alloc] initWithFrame:CGRectMake(40 + lineOneWidth, 10, lineOneWidth, 20)];
//        [self setLabelContent:_labelMimaShiyong labelString:[NSString stringWithFormat:@"使用：%@",model.mima_ok_num]];
//        [_viewMima addSubview:_labelMimaShiyong];
//    }
//    
//    [self addSubview:_viewMima];
//#pragma mark - 间隔线
    _imageViewUnderLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, _viewShop.allHeight, kScreenSize.width, 10)];
    _imageViewUnderLine.backgroundColor = kColorFrom0x(0xf5f5f5);
    [self addSubview:_imageViewUnderLine];
    
    [self labelTelAndDetailSetting];
}

- (void)setLabelContent:(UILabel *)label labelString:(NSString *)labelString{
    label.font = [UIFont systemFontOfSize:13];
    label.text = labelString;
}


- (void)labelTelAndDetailSetting{
    UITapGestureRecognizer *tapRecognizerTel=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(callFun)];
    
    _labelTel.textColor = kColorFrom0x(0x007AFF);
    _labelTel.userInteractionEnabled=YES;
    [_labelTel addGestureRecognizer:tapRecognizerTel];
    
    
    UITapGestureRecognizer *tapRecognizerDetail=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(seeDetailFunc)];
    
//    _labelWifiDetail.userInteractionEnabled=YES;
//    [_labelWifiDetail addGestureRecognizer:tapRecognizerDetail];
//    
//    _labelWifiDetail.textColor = kGreenColor;
    
    _labelJinOrZuo.userInteractionEnabled=YES;
    [_labelJinOrZuo addGestureRecognizer:tapRecognizerDetail];
    
    _imageViewHeader.layer.masksToBounds = YES;
    _imageViewHeader.layer.cornerRadius = 10;

    //今日设备详情
    if (_labelPowerDetail) {
        UITapGestureRecognizer *tapRecognizerpowerDetail=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(powerDetail)];
        _labelPowerDetail.userInteractionEnabled = YES;
        [_labelPowerDetail addGestureRecognizer:tapRecognizerpowerDetail];
    }
    
}


- (UIViewController *)viewController {
    UIResponder *next = self.nextResponder;
    do {
        //判断响应者是否为视图控制器
        if ([next isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)next;
        }
        next = next.nextResponder;
    } while (next != nil);
    
    return nil;
}

- (void)powerDetail{
    NSString *message = @"1、WiFi设备\n每台设备不足在线120分钟\n2、密码设备\n店铺内密码设备，使用率低于80%\n3、判定维护成功\n在以上2点基础上，全部WiFi设备必须被APP【扫码充电】功能使用过";
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    //paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragraphStyle.alignment = NSTextAlignmentLeft;
    //行间距
    paragraphStyle.lineSpacing = 5.0;
    NSDictionary * attributes = @{NSFontAttributeName : [UIFont systemFontOfSize:14.0], NSParagraphStyleAttributeName : paragraphStyle};
    NSMutableAttributedString *attributedTitle = [[NSMutableAttributedString alloc] initWithString:message];
    [attributedTitle addAttributes:attributes range:NSMakeRange(0, message.length)];
    // 下面这句话用了KVC ，将attributes的值给了alert
    [alert setValue:attributedTitle forKey:@"attributedMessage"];//attributedTitle\attributedMessage
    //end ---
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIStoryboard *storyboard = kMainStroyboard;
        RepairDetailV2ViewController *VC = [storyboard instantiateViewControllerWithIdentifier:@"RepairDetailV2ViewController"];
        VC.dateString = _dateString;
        VC.ids = _ids;
        VC.status = @"1";
        VC.jinriStatus = _jinriStatus;
        VC.storeType = _storeType;
        VC.modalPresentationStyle = UIModalPresentationOverFullScreen;
        [[self viewController] presentViewController:VC animated:NO completion:nil];
    }];
    
    [alert addAction:action];
    [[self viewController] presentViewController:alert animated:YES completion:nil];
}

- (void)callFun{
    
    NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",_labelTel.text];
    UIWebView *callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [[self viewController].view addSubview:callWebview];
}

- (void)seeDetailFunc{
    //点击查看详情
    //    kAlert(@"点击查看详情");
    UIStoryboard *storyboard = kMainStroyboard;
    RepairDetailV2ViewController *VC = [storyboard instantiateViewControllerWithIdentifier:@"RepairDetailV2ViewController"];
    VC.dateString = _dateString;
    VC.ids = _ids;
    VC.status = _status;
    VC.jinriStatus = _jinriStatus;
    VC.storeType = _storeType;
    VC.modalPresentationStyle = UIModalPresentationOverFullScreen;
    [[self viewController] presentViewController:VC animated:NO completion:nil];
}


- (CGFloat)height{
    return _imageViewUnderLine.allHeight;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
